package com.vdong.trade.trading.service.impl;

import com.alibaba.fastjson.JSON;
import com.vdong.trade.trading.common.exception.ProcessException;
import com.vdong.trade.trading.entity.dto.param.ChannelEarningCurveParamDTO;
import com.vdong.trade.trading.entity.dto.result.ChannelEarningCurveResultDTO;
import com.vdong.trade.trading.entity.mo.EarningMO;
import com.vdong.trade.trading.entity.po.FinanceParamDTO;
import com.vdong.trade.trading.entity.dto.EarningResultDTO;
import com.vdong.trade.trading.respository.TradingMOMapper;
import com.vdong.trade.trading.service.FinanceService;
import lombok.extern.slf4j.Slf4j;
import org.lamb.framework.common.util.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Optional;

import static com.vdong.trade.trading.common.enums.ProcessExceptionEnum.EB00000000;
import static com.vdong.trade.trading.common.enums.ProcessExceptionEnum.EB00000005;


/**
 * @author zhoulongbiao
 * @Date 2018/12/12 13:56
 */
@Service
@Slf4j
@com.alibaba.dubbo.config.annotation.Service
public class FinanceServiceImpl implements FinanceService {

    @Resource
    private TradingMOMapper tradingMOMapper;

    @Override
    public EarningResultDTO earnings(FinanceParamDTO param) {
        log.info("earnings,入参为:[{}]", JSON.toJSONString(param));
        EarningMO earningMO = tradingMOMapper.selectEarnings(param);
        EarningResultDTO dto = new EarningResultDTO();
        if (StringUtil.hasBlank(earningMO)) {
            dto.setUserId(Long.valueOf(param.getUserId()));
            dto.setMonthEarning(new BigDecimal("0"));
            dto.setTodayEarning(new BigDecimal("0"));
            dto.setTotalEarning(new BigDecimal("0"));
        } else {
            CopyUtil.copyObject(earningMO, dto);
        }
        return dto;
    }

    @Override
    public List<ChannelEarningCurveResultDTO> channelEarningCurve(ChannelEarningCurveParamDTO param) {
        log.info("channelEarningCurve,入参为:[{}]", JSON.toJSONString(param));
        if (StringUtil.hasBlank(param.getStartDate(), param.getEndDate())) {
            param.setEndDate(DateUtil.getDateToStr(new Date()));
        }
        return Optional.ofNullable(CopyUtil.copyList(tradingMOMapper.channelEarningCurve(param),
                ChannelEarningCurveResultDTO.class)).orElseThrow(() -> new ProcessException(EB00000005));
    }
}
